package com.zkingsoft.actions.admin.distribution;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.authority.MatrixAdapter;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.model.distribution.DisCoupon;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.distribution.DisCouponService;
import com.zkingsoft.util.StringUtils;
import com.zkingsoft.util.WebUtil;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-06-26 07:15
 */
@Controller
@RequestMapping(value = "admin/disCoupon")
public class DisCouponController extends BaseController {

	@Resource
	private DisCouponService disCouponService;

	@Resource
	private MatrixAdapter matrixAdapter;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "DisCoupon_BEV";

	public static final String fnCode = "DisCoupon";
	public static final String search = fnCode + ":search";
	public static final String edit = fnCode + ":edit";
	public static final String del = fnCode + ":del";
	public static final String add = fnCode + ":add";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(DisCoupon disCoupon,
			PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(search);
		return showList(disCouponService, disCoupon, pageVo);
	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(DisCoupon disCoupon) {
		if (disCoupon.getCouponId() != null) {
			matrixAdapter.isBtnPermitted(edit);
			disCoupon
					.setCouponId(((DisCoupon) WebUtil.getSessionAttribute(BEV))
							.getCouponId());
			AjaxResult result = modify(disCouponService,
					WebUtil.getSessionAttribute(BEV), disCoupon, "优惠券");
			WebUtil.removeSessionAttribute(BEV);
			return result;
		} else {
			matrixAdapter.isBtnPermitted(add);
			disCoupon.setCouponCreateTime(new Date());
			disCoupon.setCouponState("1");
			disCoupon.setCouponCode(StringUtils.getRandomString(10));
			return add(disCouponService, disCoupon, "优惠券");
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		matrixAdapter.isBtnPermitted(edit);
		DisCoupon disCoupon;
		if (id != null) {
			disCoupon = disCouponService.findById(id);
			WebUtil.getRequest().setAttribute("obj", disCoupon);
			WebUtil.setSessionAttribute(BEV, disCoupon);
		}
		return "admin/distribution/DisCoupon-form";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		matrixAdapter.isBtnPermitted(del);
		return remove(disCouponService, keys);
	}

}